package com.ylpms.api.controller;

import com.ylpms.too.AjaxJson;
import com.ylpms.too.RestUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

@RestController
public class CimissController {

    private final String host = "10.172.89.55";
    private final String userId = "BEXA_YGZX_sxnqzx";
    private final String pwd = "81619505";
    private final String dataFormat = "json";
    private final int timeoutInMilliSeconds = 1000 * 60 * 5; // 2 MINUTE

    @RequestMapping(value = "/getSurfEleByTimeRangeAndStaID")
    public String getSurfEleByTimeRangeAndStaID(String interfaceId,String dataCode, String staids,String elements,String timeRange) {
        AjaxJson ajaxJson = new AjaxJson();
        timeRange = "["+timeRange+"]";
        String params = "userId="+ this.userId +"&pwd="+ this.pwd +"&dataFormat="+ this.dataFormat;
        params += "&interfaceId="+ interfaceId +"&dataCode="+ dataCode +"";
        params += "&staids="+ staids +"&elements="+ elements +"&timeRange="+ timeRange +"";
        RestUtil restUtil = new RestUtil();
        String restResult = restUtil.getRestData(params);
        return restResult;
    }

    //http://10.172.89.55/cimiss-web/api?userId=BEXA_YGZX_sxnqzx&pwd=81619505&interfaceId=getSurfEleInHistoryBySamePeriodAndStaID&dataCode=SURF_CHN_MUL_DAY&elements=Station_Name,Station_Id_C,Year,Mon,Day,TEM_Avg&minYear=2018&maxYear=2019&minMD=0201&maxMD=0215&staIds=54511&dataFormat=json
    @RequestMapping(value = "/getSurfEleInHistoryBySamePeriodAndStaID")
    public AjaxJson getSurfEleInHistoryBySamePeriodAndStaID(String interfaceId,String dataCode, String staids,String elements,
                                                          String minYear,String maxYear,String minMD,String maxMD){
        AjaxJson ajaxJson = new AjaxJson();
        String params = "userId="+ this.userId +"&pwd="+ this.pwd +"&dataFormat="+ this.dataFormat;
        params += "&interfaceId="+ interfaceId +"&dataCode="+ dataCode +"";
        params += "&staIds="+ staids +"&elements="+ elements +"&minYear="+ minYear +"&maxYear="+ maxYear+"&minMD="+ minMD +"&maxMD="+ maxMD +"";
        System.out.print(params);
        System.out.print("\r\n====================================\r\n");
        RestUtil restUtil = new RestUtil();
        String restResult = restUtil.getRestData(params);
        System.out.print(restResult);
        JSONObject jsonObject=JSONObject.fromObject(restResult);
        JSONArray ja = jsonObject.getJSONArray("DS");
        String [] elementArr;
        if(elements.indexOf(",") != -1){
            elementArr = elements.split(",");
        }else{
            elementArr = new String[]{elements};
        }
        Map<String,Map<String,Object[]>> resMap = new HashMap<String,Map<String,Object[]>>();
        for(int i=0;i<elementArr.length;i++){
            if(elementArr[i].equals("Year") || elementArr[i].equals("Mon") || elementArr[i].equals("Day")) continue;
            if(!resMap.containsKey(elementArr[i])){
                resMap.put(elementArr[i],new HashMap<String, Object[]>());
            }
            JSONObject jo = null;
            for(int n=0;n<ja.size();n++){
                //System.out.println(n);
                jo = ja.getJSONObject(n);
                String y = jo.getString("Year");
                String m = jo.getString("Mon");
                String d = jo.getString("Day");
                double v = Double.parseDouble(jo.getString(elementArr[i]));
                if(!resMap.get(elementArr[i]).containsKey(m+"-"+d)){
                    resMap.get(elementArr[i]).put(m+"-"+d,new Object[]{v,y});
                }else{
                    double z = Double.parseDouble(resMap.get(elementArr[i]).get(m+"-"+d)[0].toString())+v;
                    resMap.get(elementArr[i]).put(m+"-"+d,new Object[]{z,y});
                }
            }
        }
        ajaxJson.setObj(resMap);
        return ajaxJson;
    }

}
